package com.zhaungjie.base.utils;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * LocalTime的运算函数
 *
 * @author zhuangjie
 * @date 2023/11/25
 */
public class LocalTimeOperations implements Comparable<LocalTimeOperations>{
    private LocalTime localTime = LocalTime.now();
    private static final String TIME_SEPARATOR = ":";

    public LocalTimeOperations(String baseFormat) {
        update(baseFormat);
    }

    public LocalTimeOperations(LocalTime localTime) {
        setCore(localTime);
    }

    /**
     * 复制实例(返回创建的新实例TimeOperations)
     *
     * @return {@link LocalTimeOperations}
     */
    public LocalTimeOperations copyInstance() {
        return new LocalTimeOperations(LocalTime.of(this.localTime.getHour(), this.localTime.getMinute(), this.localTime.getSecond()));
    }

    public LocalTimeOperations setCore(LocalTime localTime) {
        this.localTime = localTime;
        return this;
    }
    public LocalTime getCore() {
        return this.localTime;
    }

    /**
     * 数组填充（通过分割得到有值的非等长数组填充到预定长度数组中）
     *
     * @param preSize        预先确定尺寸
     * @param initElementVal 初始化元素val
     * @param template       样板
     * @param separator      分离器
     * @return {@link List}<{@link Integer}>
     */
    private List<Integer> arrayFillForSplit(Integer preSize, Integer initElementVal, String template, String separator) {
        String[] arr = template.split(separator);
        List<Integer> result = new ArrayList<>(Collections.nCopies(preSize,initElementVal));
        for (Integer i = 0; i < preSize; i++) {
            Integer temp = initElementVal;
            try {
                temp = Integer.valueOf(arr[i]);
            }catch (ArrayIndexOutOfBoundsException e) {}
            result.set(i,temp);
        }
        return result;
    }


    /**
     * 设定时间(传入HH:mm:ss)
     *
     * @param baseFormat 基本格式
     * @return {@link LocalTimeOperations}
     */
    public LocalTimeOperations update(String baseFormat) {
        if (baseFormat == null || baseFormat.trim().isEmpty() ) return this;
        List<Integer> hmsArr = arrayFillForSplit(3, 00, baseFormat, TIME_SEPARATOR);
        Integer hh = hmsArr.get(0),mm = hmsArr.get(1),ss = hmsArr.get(2);
        this.localTime = this.localTime.withHour(hh);
        this.localTime = this.localTime.withMinute(mm);
        this.localTime = this.localTime.withSecond(ss);
        return this;
    }

    public LocalTimeOperations addHour(Integer hour) {
        if (hour != null )  this.localTime = this.localTime.plusHours(this.localTime.getHour()+hour);
        return this;
    }
    public LocalTimeOperations addMinute(Integer minute) {
        if (minute != null )  this.localTime = this.localTime.plusMinutes(this.localTime.getMinute()+minute);
        return this;
    }
    public LocalTimeOperations addSecond(Integer second) {
        if (second != null )  this.localTime = this.localTime.plusSeconds(this.localTime.getSecond()+second);
        return this;
    }

    /**
     * 格式化为字符串
     * @param formatTemplate 一段字符串（HH代表小时、mm代表分钟、ss代表秒钟）
     * @return
     */
    public String format(String formatTemplate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatTemplate);
        return  this.localTime.format(formatter);
    }

    // 转number
    public Integer toNumberValue() {
        int hour = this.localTime.getHour();
        int minute = this.localTime.getMinute();
        int second = this.localTime.getSecond();
        return second + minute * 60  + hour*60*60;
    }

    @Override
    public int compareTo(LocalTimeOperations localTimeOperations2) {
        if (localTimeOperations2 == null) return this.toNumberValue() - 0;
        return this.toNumberValue() - localTimeOperations2.toNumberValue();
    }



}
